package com.hyjy.music.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager2.widget.ViewPager2
import com.hyjy.music.R

class MyViewPagerIndicator(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {

    constructor(context: Context) : this(context, null)

    private val llContainer by lazy {
        LinearLayout(context)
    }

    private val ivMovePoint by lazy {
        ImageView(context)
    }

    private val shapeMoving by lazy {
        GradientDrawable().apply {
            shape = GradientDrawable.OVAL
            setColor(colorMoving)
            setSize(pointSize, pointSize)
            ivMovePoint.background = this
        }
    }

    private val shapeDefault by lazy {
        GradientDrawable().apply {
            shape = GradientDrawable.OVAL
            setColor(colorDefault)
            setSize(pointSize, pointSize)
        }
    }

    private var colorDefault = 0
    private var colorMoving = 0
    private var pointSize = 0
    private var distance = 0

    init {
        context.obtainStyledAttributes(attrs, R.styleable.ViewPagerIndicator).apply {
            colorDefault = getColor(R.styleable.ViewPagerIndicator_pointColor, Color.WHITE)
            colorMoving = getColor(R.styleable.ViewPagerIndicator_movePointColor, Color.WHITE)
            pointSize = getDimension(R.styleable.ViewPagerIndicator_pointSize, 5.0f).toInt()
            recycle()
        }
        FrameLayout(context).apply {
            ivMovePoint.layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            this.addView(llContainer)
            this.addView(ivMovePoint)
            this@MyViewPagerIndicator.addView(this)
        }
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        if (llContainer.childCount < 2) {
            return
        }
        distance = llContainer.getChildAt(1).left - llContainer.getChildAt(0).left
    }

    fun setViewPager(viewPager: ViewPager2?) {
        viewPager?.adapter?.let {
            llContainer.removeAllViews()
            shapeMoving
            shapeDefault
            var imageView: ImageView
            var params: LinearLayout.LayoutParams
            for (i in 0 until it.itemCount) {
                // 向LinearLayout中添加小圆点
                imageView = ImageView(context)
                imageView.background = shapeDefault
                // 第一个点不设置左边距
                params = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
                params.leftMargin = if (i > 0) pointSize else 0
                imageView.layoutParams = params
                llContainer.addView(imageView)
            }
            viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {
                    super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                    if (llContainer.childCount < 2) {
                        return
                    }
                    ivMovePoint.translationX = distance * (positionOffset + position)
                }
            })
        }
    }
}